import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zyj
 * @date 2022年06月11日 11:01
 */
public class Server {
    public static void main(String[] args) {
        ServerSocket server = null;
        //从控制台输入消息  类似于Scanner
        BufferedReader input = null;
        //打印控制台的流
        PrintStream print = null;
        //接收消息的流
        BufferedReader accept = null;
        //发送消息
        PrintStream send = null;
        try {
            server = new ServerSocket(80);
            //等待客户端的连接
            System.out.println("服务端已经启动，等待客户端的连接");
            Socket socket = server.accept();
            input = new BufferedReader(new InputStreamReader(System.in));
            print = new PrintStream(System.out);
            //创建接收消息以及发送消息的流对象
            accept = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            send = new PrintStream(socket.getOutputStream());

            boolean flag = true;
            while (flag) {
                //先接收客户端发送的消息
                String mess = accept.readLine();
                if (null == mess || "".equals(mess)){
                    flag = false;
                }else if ("exit".equals(mess)){
                    flag = false;
                }else {
                    print.println(mess);
                    //往客户端发送消息
                    print.println("请输入发送到客户端的消息");
                    String message = input.readLine();
                    send.println(message);
                }
            }
            System.out.println(socket);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
